var categoryDao = require('../daos/categoryDao'),
	idService = require('./idService');

// -------------------------- CRUD SERVICES ----------------------------

module.exports.getAll = function (short, callback) {
	categoryDao.getAll(short, callback);
}

module.exports.find = function (filters, callback) {
	categoryDao.find(filters, callback);
}

module.exports.findOne = function (filters, callback) {
	categoryDao.findOne(filters, callback);
}

module.exports.save = function (category, callback) {
	if (!category.id) {
		idService.getId('categories', function(err, categoryId) {
			if (err) {
				callback(err);
				return;
			}
			if (categoryId) {
				category.id = categoryId;
				categoryDao.upsert(category.id, category, function(err) {
					if (err)
						callback(err);
					else
						callback(false, category)
				});
			} else {
				console.log('ERROR: categoryService save. Detalle: ' + err);
		  	  	callback('category getAutoId fail');
			}
		});
	} else {
		categoryDao.upsert(category.id, category, function(err) {
			if (err)
				callback(err);
			else
				callback(false, category);
		});
	}
}

module.exports.remove = function (id, callback) {
	categoryDao.remove(id, callback);
}

module.exports.drop = function () {
	return categoryDao.drop();
}
